﻿$(function () {
    toastr.options.positionClass = 'toast-bottom-center';

    // 自定义验证
    var checkIntergor = (rule, value, callback) => {
        if (!value || value===0) {
            return callback(new Error('不能为空,且大于0'))
        }
        if (!Number.isInteger(Number(value))) {
            return callback(new Error('必须为整数'));
        }
        callback();
    };

    var vm = new Vue({
        el: "#app",
        data: {
            filterName: '',
            dialogVisible: false,
            dialogTitle: '创建科室',
            tableData: [],
            formLabelWidth: '120px',
            actionType :'新建',
            department: {
                id: null,
                name: null,
                code: null,
                outerCoder: null,
                isActive: true,
                order: null,
                contactPerson: null,
                phoneNumber: null,
                desc: null,
            },
            rules: {
                name: [
                    { required: true, message: '请输入科室名', trigger: 'blur' }
                ],
                code: [
                    {required:true,message:'请输入编码',trigger:'blur'}
                ],
                outerCode: [
                    {required:true,message:'请输入外码',trigger:'blur'}
                ],
                order: [
                    {required:true,message:'请输入顺序',trigger:'blur'}
                ],
                appointableCount: [
                    { required: true, message: '请输入可预约天数', trigger: 'blur' },
                    { validator: checkIntergor, trigger: 'blur' }
                ],
                earliest: [
                    { required: true, message: '请输入提前预约时限', trigger: 'blur' },
                    { validator: checkIntergor, trigger: 'blur' }
                ]

            }
        },
        methods: {

            onOuterCodeChange(val) {
                const vm = this;
                vm.department.code = val;
            },

            // 加载列表数据
            loadTableData() {
                var vm = this;
                abp.ajax({
                    type: 'GET',
                    url: `/api/department/get-list?filterName=${vm.filterName}`
                }).then((res) => {
                    if (res.code === 0) {
                        vm.tableData = res.data;
                    }     
                })
            },

            // 删除
            deleteDep(index,row) {
                var vm = this;
                abp.message.confirm(
                    `确定要删除 ${row.name}?`,
                    '删除科室',
                    (confirmed)=>{
                        if (!confirmed) {
                            return;
                        }
                        else {
                            abp.ajax({
                                type: 'GET',
                                url:`/api/department/delete?id=${row.id}`
                            }).then((res) => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.loadTableData();
                                }
                            })
                        }
                    }
                );
            },

            // 点击新建
            showCreate() {
                var vm = this;
                Object.keys(vm.department).forEach(key => {
                    vm.department[key] = null;
                });
                vm.actionType = '新建';
                vm.department.isActive = true;
                vm.department.order = 0;
                vm.dialogVisible = true;
                vm.dialogTitle = '新建科室';
                vm.department.appointableCount = 30;
                vm.department.earliest = 30;
            },

            // 点击编辑
            showEdit(index,row) {
                var vm = this;
                vm.actionType = '编辑';
                vm.dialogTitle = "编辑科室";
                vm.dialogVisible = true;
                // 获取科室信息
                vm.getSingle(row.id);
            },

            // 获取部门
            getSingle(id) {
                var vm = this;
                abp.ajax({
                    type: 'GET',
                    url: `/api/department/get?id=${id}`
                }).then(res => {
                    if (res.code === 0) {
                        vm.department = res.data;
                    }
                })
            },

            // 保存
            saveDep() {
                var vm = this;
                if (vm.actionType === '新建') {
                    vm.createSubmit();
                }
                else { // 编辑
                    vm.editSubmit();
                }
            },

            // 创建提交
            createSubmit() {
                var vm = this;
                dep = vm.department;
                vm.$refs['dialogForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'POST',
                            data: JSON.stringify({
                                name: dep.name,
                                code: dep.code,
                                outerCode: dep.outerCode,
                                isActive: dep.isActive,
                                order: dep.order,
                                contactPerson: dep.contactPerson,
                                phoneNumber: dep.phoneNumber,
                                desc: dep.desc
                            }),
                            url: '/api/department/create',
                            dataType: 'json',
                            contentType: 'application/json'
                        }).then((res) => {
                            if (res.code === 0) {
                                abp.notify.success(res.msg)
                                vm.resetForm();
                                vm.loadTableData();
                            }
                        });
                    }
                });
            },

            // 编辑提交
            editSubmit() {
                var vm = this;
                vm.$refs['dialogForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'POST',
                            data: JSON.stringify(vm.department),
                            url: '/api/department/update'
                        }).then((res) => {
                            if (res.code === 0) {
                                vm.loadTableData();
                                abp.notify.success(res.msg);
                                vm.resetForm();
                            }
                        });
                    }
                });
            },

            // 表单重置
            resetForm() {
                var vm = this;
                vm.$refs['dialogForm'].resetFields();
                vm.dialogVisible = false;
            }
        },
        created(){
            var vm = this;
            vm.loadTableData();
        },
        mounted(){

        }
    });
})